package com.caiyouxi.finance.service;

import com.caiyouxi.common.Constant;
import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.finance.mapper.FinanceTradeDetailMapper;
import com.caiyouxi.finance.model.FinanceTradeDetail;
import com.caiyouxi.order.model.OrderBean;
import com.caiyouxi.util.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;


/**
 * 财务流水记录
 * chensong
 */
@Service
public class FinanceTradeDetailService extends BaseService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private FinanceTradeDetailMapper financeTradeDetailMapper;

    public FinanceTradeDetail add_finance_trade_details(String orderId,String currency_code,String target_type,String account_code,Integer buy_user_id,Integer sell_user_id,Integer totalAmount,String title){
        FinanceTradeDetail ftd = new FinanceTradeDetail();
        ftd.setId(new BigInteger("8" + NumUtil.generate_number(5)));
        ftd.setTarget_type(target_type);//业务对象类型
        ftd.setTarget_id(orderId);//业务对象编号
        ftd.setCurrency_code(currency_code);//货币类型
        ftd.setAccount_code(account_code);//专用账号代码
        ftd.setPayer(buy_user_id);//付款人
        ftd.setPayee(sell_user_id);//收款人
        ftd.setTotal_amount(totalAmount);//款项总金额
        ftd.setFinished_amount(0);//完成金额。已经支付的金额   后面更新
        ftd.setTitle(title);//明细标题
        ftd.setStatus(Constant.FINANCE_STATUS_PAYABLE);//应付的（但是未收）
        ftd.setCreate_time(new Date());
        ftd.setUpdate_time(new Date());
        financeTradeDetailMapper.add_finance_trade_details(ftd);
        return ftd;
    }

    /**
     * 查询
     */
    public FinanceTradeDetail select_finance_trade_details_by_map(HashMap<String,Object> map){
        return financeTradeDetailMapper.select_finance_trade_details_by_map(map);
    }

    /**
     * 更新
     */
    public void update_finance_trade_details(FinanceTradeDetail ftd){
        financeTradeDetailMapper.update_finance_trade_details(ftd);
    }

    /**
     * 更新交易流水记录信息状态
     */
    public FinanceTradeDetail updateFinanceTradeDetail(OrderBean order, String status,String target_type){
        HashMap<String,Object> map = new HashMap();
        map.put("target_id",order.getId());
        map.put("target_type",target_type);
        FinanceTradeDetail ftd = select_finance_trade_details_by_map(map);
        //更新交易流水
        ftd.setStatus(status);
        ftd.setFinished_amount(ftd.getTotal_amount());
        update_finance_trade_details(ftd);
        return ftd;
    }

    /**
     * 查询所有，分页
     * wushihao
     * */
    public ResData get_financetrade_list(FinanceTradeDetail bean){
        QueryParams pageList = new QueryParams();
        if(bean.getPage_size() == 0){
            bean.setPage_size(Integer.parseInt(redisService.get("sys.pagesize").toString()));
        }
        pageList.setPage_size(bean.getPage_size());
        pageList.setPage(bean.getPage());
        pageList.setList(financeTradeDetailMapper.get_financetrade_list(bean));
        pageList.setTotal(financeTradeDetailMapper.get_financetrade_counts(bean));
        return success(pageList);
    }
}
